import QtQuick 2.7
import QtQuick.Controls 2.0  //控制版本
import QtQuick.Layouts 1.0  //布局版本
import an.qt.ColorMaker 1.0  //导入自定义的C++类

ApplicationWindow {
    visible: true
    width: 640
    height: 480
    title: qsTr("qml与C++混合编程-20200612")
    Rectangle{
        width: 360;
        height: 360;
        //显示时间文本的label
        Text{
            id:timeLabel;
            anchors.left: parent.left;
            anchors.leftMargin: 4;
            anchors.top:parent.top;
            anchors.topMargin: 4;
            font.pixelSize: 26;
        }

        //这里调用了C++类，创建了一个供QML使用的C++对象
        ColorMaker{
            id:colorMaker;
            color:Qt.green;
        }
        //中间显示颜色变化的矩形
        Rectangle{
            id:colorRect;
            anchors.centerIn: parent;
            width: 200;
            height: 200;
            color: "blue";
            Text{
                id:str_color_algorithm;
                anchors.fill: colorRect;
                anchors.centerIn: colorRect;
                text:"sdasdasd";
            }
        }
        //开始按钮
        Button{
            id:start;
            text:"start";
            anchors.left:parent.left;
            anchors.leftMargin: 4;
            anchors.bottom: parent.bottom;
            anchors.bottomMargin: 4;
            onClicked: {
                colorMaker.start();
            }
        }
        //结束按钮
        Button{
            id:stop;
            text:"stop";
            anchors.left: start.right;
            anchors.leftMargin: 4;
            anchors.bottom: start.bottom;
            onClicked: {
                colorMaker.stop();
            }
        }


        //改变颜色算法按钮
        Button{
            id:bt_colorAlgorithm;
            text:"RGB";
            anchors.left: stop.right;
            anchors.leftMargin: 4;
            anchors.bottom: start.bottom;
            onClicked: {
                var color_algorithm = (colorMaker.getAlgorithm()+1)%5;
                changeAlgorithm(color_algorithm);
                //str_color_algorithm.text = color_algorithm.toString();
                colorMaker.setAlgorithm(color_algorithm);
            }
            function changeAlgorithm(algorithm){
                switch(algorithm)
                {
                case 0:
                    str_color_algorithm.text = "RandomRGB";
                    break;
                case 1:
                    str_color_algorithm.text = "RandomRed";
                    break;
                case 2:
                    str_color_algorithm.text = "RandomGreen";
                    break;
                case 3:
                    str_color_algorithm.text = "RandomBlue";
                    break;
                case 4:
                    str_color_algorithm.text = "LinearIncrease";
                    break;
                }
            }
        }
        //JavaScript函数


        //退出按钮
        Button{
            id:quit;
            text:"quit";
            anchors.left: bt_colorAlgorithm.right;
            anchors.leftMargin: 4;
            anchors.bottom: start.bottom;
            onClicked: {
                Qt.quit();
            }
        }

        Component.onCompleted: {
            colorMaker.color = Qt.rgba(0,180,120,255);
            colorMaker.setAlgorithm(ColorMaker.LinearIncrease);
            str_color_algorithm.text = "LinearIncrease";
        }
        //关联控件colorMaker和槽函数onCurrentTimer
        Connections{
            target: colorMaker;  //接收事件的对象
            onCurrentTimer:{  //接收事件时的处理函数
                timeLabel.text = strTime;  //strTime是currentTimer信号里的参数
                timeLabel.color = colorMaker.timeColor;
            }
        }
        Connections{
            target: colorMaker;
            onColorChanged:{
                colorRect.color = color;
            }
        }
    }
}
